package com.kmproject.haifu.mvp.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jess.arms.base.BaseActivity;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.http.imageloader.glide.ImageConfigImpl;
import com.jess.arms.utils.ArmsUtils;
import com.jess.arms.utils.DataHelper;
import com.jess.arms.widget.autolayout.AutoToolbar;
import com.kmproject.haifu.R;
import com.kmproject.haifu.app.AppConstant;
import com.kmproject.haifu.di.component.DaggerMemberCenterUpdateLevelComponent;
import com.kmproject.haifu.mvp.contract.MemberCenterUpdateLevelContract;
import com.kmproject.haifu.mvp.model.entity.beans.MemberCenterUserInfoBean;
import com.kmproject.haifu.mvp.model.entity.beans.UserBean;
import com.kmproject.haifu.mvp.presenter.MemberCenterUpdateLevelPresenter;
import com.kmproject.haifu.mvp.ui.adapter.FragmentTitlesAdapter;
import com.kmproject.haifu.mvp.ui.adapter.MemberUpdateLevelFeatureAdapter;
import com.kmproject.haifu.mvp.ui.adapter.MemberUpdateLevelRuleAdapter;
import com.kmproject.haifu.mvp.ui.fragment.MyCollectHouseRentFragment;
import com.kmproject.haifu.mvp.ui.fragment.MyCollectSecondHandHouseFragment;
import com.kmproject.haifu.mvp.ui.widget.CircleImageView;
import com.kmproject.haifu.mvp.ui.widget.NoScrollViewPager;
import com.kmproject.haifu.mvp.ui.widget.ScaleTransitionPagerTitleView;

import net.lucode.hackware.magicindicator.MagicIndicator;
import net.lucode.hackware.magicindicator.ViewPagerHelper;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.CommonNavigator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.CommonNavigatorAdapter;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerTitleView;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.indicators.LinePagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.SimplePagerTitleView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

import static com.jess.arms.utils.Preconditions.checkNotNull;


/**
 * ================================================
 * Description:
 * <p>
 * Created by MVPArmsTemplate on 07/20/2019 11:46
 * <a href="mailto:jess.yan.effort@gmail.com">Contact me</a>
 * <a href="https://github.com/JessYanCoding">Follow me</a>
 * <a href="https://github.com/JessYanCoding/MVPArms">Star me</a>
 * <a href="https://github.com/JessYanCoding/MVPArms/wiki">See me</a>
 * <a href="https://github.com/JessYanCoding/MVPArmsTemplate">模版请保持更新</a>
 * ================================================
 */
public class MemberCenterUpdateLevelActivity extends BaseActivity<MemberCenterUpdateLevelPresenter> implements MemberCenterUpdateLevelContract.View {

    @BindView(R.id.toolbar_backImg)
    ImageView toolbarBackImg;
    @BindView(R.id.toolbar_back)
    RelativeLayout toolbarBack;
    @BindView(R.id.toolbar_title)
    TextView toolbarTitle;
    @BindView(R.id.back_right_icon)
    ImageView backRightIcon;
    @BindView(R.id.back_right_txt)
    TextView backRightTxt;
    @BindView(R.id.toolbar_right)
    RelativeLayout toolbarRight;
    @BindView(R.id.toolbar)
    AutoToolbar toolbar;
    @BindView(R.id.MemberCenterMain_header_img)
    CircleImageView MemberCenterMainHeaderImg;
    @BindView(R.id.MemberCenterMain_Uname_txt)
    TextView MemberCenterMainUnameTxt;
    @BindView(R.id.MemberCenterMain_dl_img)
    ImageView MemberCenterMainDlImg;
    @BindView(R.id.MemberCenterMain_yfb_fyesf)
    TextView MemberCenterMainYfbFyesf;
    @BindView(R.id.MemberCenterMain_bg)
    LinearLayout MemberCenterMainBg;
    @BindView(R.id.member_center_updatelevel_indicator)
    MagicIndicator memberCenterUpdatelevelIndicator;
    @BindView(R.id.MyCollect_viewpager)
    NoScrollViewPager MyCollectViewpager;
    MemberCenterUserInfoBean memberCenterUserInfoBean;


    @Override
    public void setupActivityComponent(@NonNull AppComponent appComponent) {
        DaggerMemberCenterUpdateLevelComponent //如找不到该类,请编译一下项目
                .builder()
                .appComponent(appComponent)
                .view(this)
                .build()
                .inject(this);
    }

    @Override
    public int initView(@Nullable Bundle savedInstanceState) {
        return R.layout.activity_member_center_update_level; //如果你不需要框架帮你设置 setContentView(id) 需要自行设置,请返回 0
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {
        toolbarTitle.setTextColor(ArmsUtils.getColor(this, R.color.white));
        toolbarBackImg.setImageResource(R.drawable.member_center_main_back_img);
        toolbar.setBackgroundColor(ArmsUtils.getColor(this, R.color.color_member_bg));
        memberCenterUserInfoBean=(MemberCenterUserInfoBean)getIntent().getSerializableExtra("MemberCenterUserInfoBean");
       if (memberCenterUserInfoBean==null)
       {
           showMessage("数据异常");
           killMyself();
           return;
       }
        initGlide();
        iniUser();
        getMglideImage().loadImage(this, ImageConfigImpl.builder().url(memberCenterUserInfoBean.getMemberLevel().getHeadImage())
                .imageView(MemberCenterMainDlImg)
                .build());
        MemberCenterMainYfbFyesf.setText("已发布房源"+memberCenterUserInfoBean.getHouseRentReleaseCount()+"个，二手物品"+memberCenterUserInfoBean.getSecondHandReleaseCount()+"个");
        mPresenter.getMemberLevelList();
    }
    /**
     * 初始化显示用户的信息
     */
    public void iniUser() {
        //用户已经登录
        UserBean userBean = DataHelper.getDeviceData(this, AppConstant.USER_INFO);//获取用户信息
        if (userBean != null) {
            if (userBean.getAvatar() != null && !TextUtils.isEmpty(userBean.getAvatar())) {
                getMglideImage().loadImage(this, ImageConfigImpl.builder().url(userBean.getAvatar()).imageRadius(5)
                        .imageView(MemberCenterMainHeaderImg)
                        .build());
            } else {
                MemberCenterMainHeaderImg.setImageResource(R.drawable.default_headimage);
            }

            MemberCenterMainUnameTxt.setText((userBean.getNickName() == null || TextUtils.isEmpty(userBean.getNickName())) ? "您还没设置昵称" : userBean.getNickName());
        } else {
            MemberCenterMainHeaderImg.setImageResource(R.drawable.default_headimage);
        }

    }
    @Override
    public void showLoading() {
            loadingDialog.show();
    }

    @Override
    public void hideLoading() {
            loadingDialog.dismiss();
    }

    @Override
    public void showMessage(@NonNull String message) {
        checkNotNull(message);
        ArmsUtils.snackbarText(message);
    }

    @Override
    public void launchActivity(@NonNull Intent intent) {
        checkNotNull(intent);
        ArmsUtils.startActivity(intent);
    }

    @Override
    public void killMyself() {
        finish();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @Override
    public void getMemberLevelListSuccess(List<MemberCenterUserInfoBean.MemberLevelBean> memberLevelBeans) {

        MyCollectViewpager.setAdapter(new FragmentTitlesAdapter(getSupportFragmentManager(), this, memberLevelBeans));

        CommonNavigator commonNavigator = new CommonNavigator(this);
//        final  int defaultWidth=getViewHeight(MyCollectMagicIndicator,false);

        commonNavigator.setAdapter(new CommonNavigatorAdapter() {
            @Override
            public int getCount() {
                return memberLevelBeans.size();
            }

            @Override
            public IPagerTitleView getTitleView(Context context, final int index) {

                SimplePagerTitleView simplePagerTitleView = new ScaleTransitionPagerTitleView(context);
                simplePagerTitleView.setText(memberLevelBeans.get(index).getLevelName());
//                DisplayMetrics dm = getResources().getDisplayMetrics();
//                showMessage(dm.widthPixels+"");
//                if (dm.widthPixels>1000)
//                {
//                    simplePagerTitleView.setWidth((dm.widthPixels)/memberLevelBeans.size());
//                }
                simplePagerTitleView.setTextSize(14);
                simplePagerTitleView.setNormalColor(getResources().getColor(R.color.color_191919));
                simplePagerTitleView.setSelectedColor(getResources().getColor(R.color.color_191919));
                LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
//                layoutParams.setMargins(15, 0, 5, 0);//4个参数按顺序分别是左上右下
                simplePagerTitleView.setLayoutParams(layoutParams);
                simplePagerTitleView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        MyCollectViewpager.setCurrentItem(index);
                    }
                });

                return simplePagerTitleView;
            }

            @Override
            public IPagerIndicator getIndicator(Context context) {
                LinePagerIndicator linePagerIndicator = new LinePagerIndicator(context);
                linePagerIndicator.setMode(LinePagerIndicator.MODE_WRAP_CONTENT);
                linePagerIndicator.setLineHeight(8.0f);
                linePagerIndicator.setColors(ArmsUtils.getColor(MemberCenterUpdateLevelActivity.this, R.color.color_F9D35A));

                return linePagerIndicator;
            }

            @Override
            public float getTitleWeight(Context context, int index) {
                return 1.0f;
            }

        });
        memberCenterUpdatelevelIndicator.setNavigator(commonNavigator);
        ViewPagerHelper.bind(memberCenterUpdatelevelIndicator, MyCollectViewpager);
        int defaultSelectIndex=mPresenter.getSelectIndex(memberLevelBeans,memberCenterUserInfoBean);
        MyCollectViewpager.setCurrentItem(defaultSelectIndex);
        memberCenterUpdatelevelIndicator.onPageSelected(defaultSelectIndex);
        //预加载管理
        MyCollectViewpager.setOffscreenPageLimit(0);
    }

    public MemberCenterUserInfoBean getMemberCenterUserInfoBean() {
        return memberCenterUserInfoBean;
    }

}
